package com.cartrentalsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cartrentalsystem.domain.po.Customers;
import com.cartrentalsystem.domain.vo.Result;
import com.cartrentalsystem.service.ICustomersService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LHD
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/customers")
@RequiredArgsConstructor
@Slf4j
public class CustomersController {

    private final ICustomersService customersService;

    @GetMapping("/list")
    public Result<Page<Customers>> pageList(@Param("page") Integer page,
                                            @Param("size") Integer size,
                                            @RequestParam(value = "name",required = false) String name,
                                            @RequestParam(value = "phone",required = false)String phone,
                                            @RequestParam(value = "cartNumber",required = false) String cartNumber,
                                            @RequestParam(value = "status",required = false) Integer status) {
        log.info("page: {}, size: {},name:{},phone={},carNumber={},status:{}", page, size, name,phone,cartNumber,status);
        log.info("查询所有用户列表");
        Page<Customers> list = customersService.pageList(page, size, name, phone, cartNumber, status);
        log.info("用户列表查询成功:{}",  list);
        return Result.success(list);
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody Customers customers) {
        log.info("添加用户:{}",  customers);
        boolean save = customersService.saveCustomer(customers);
        log.info("用户添加成功");
        return save ? Result.success("添加成功") : Result.fail("添加失败");
    }

    // 逻辑删除
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        log.info("删除用户:{}",  id);
        boolean removeById = customersService.removeById(id);
        log.info("用户删除成功");
        return removeById ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @GetMapping("/names")
    public Result<String[]> getNames() {
        List<String> names = customersService.list().stream()
                .filter(customers -> customers.getDeleted() == 0)
                .filter(customers -> customers.getStatus() == 1)
                .map(Customers::getName).toList();
        return Result.success(names.toArray(new String[0]));
    }

    /**
     * 修改顾客信息
     * @param customerId
     * @param customers
     * @return
     */
    @PutMapping("/updateCustomer/{customerId}")
    @ApiOperation(value = "修改顾客信息")
    public Result<String> updateCustomer(@PathVariable Integer customerId, @RequestBody Customers customers) {
        customers.setId(customerId);
        log.info("修改顾客信息:{}",  customers);
        return customersService.updateCustomer(customers) ? Result.success("修改成功") : Result.fail("修改失败");
    }

}
